package com.woniuxy.controller;

import com.woniuxy.dao.MaintainRecordMapper;
import com.woniuxy.pojo.*;
import com.woniuxy.service.MaintainRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.MaintainRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MaintainRecordController extends BaseController{
    @Autowired
    private MaintainRecordService maintainRecordService;
    @Autowired
    private MaintainRecordMapper maintainRecordMapper;
    @GetMapping(value = "/maintainRecords")
    public DataGridView selectMaintainRecordsByProperty(MaintainRecordVo maintainRecordVo) {
        return maintainRecordService.selectMaintainRecordsByProperty(maintainRecordVo);
    }

    @DeleteMapping(value = "/maintainRecords/{id}")
    public ResultDto deleteMaintainRecordById(@PathVariable("id") int id) {
        try {
            this.maintainRecordService.deleteMaintainRecordById(id);
            return ResultDto.DELETE_MR_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_MR_FAILURE;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/maintainRecords/deleteBathMaintainRecord")
    public ResultDto removeMaintainRecord(@RequestParam("ids[]") Integer[] ids) {
        try {
            this.maintainRecordService.removeMaintainRecord(ids);
            return ResultDto.DELETE_MR_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_MR_FAILURE;
        }
    }
    @GetMapping("/maintainRecords/cars.do")
    public List<Car> cars() {
        return maintainRecordMapper.selectAllCars();
    }

    @GetMapping("/maintainRecords/units.do")
    public List<CurrentUnit> selectAllUnit() {
        return maintainRecordMapper.selectAllUnit();
    }

    @GetMapping("/maintainRecords/users.do")
    public List<User> users() {
        return maintainRecordMapper.selectAllUsers();
    }
    @GetMapping("/maintainRecords/dictionaries.do")
    public List<Dictionary> dictionaries() {
        return maintainRecordMapper.selectAllMintains();
    }
    @PostMapping(value = "/maintainRecords.do")
    public ResultDto addMaintainRecord(MaintainRecord maintainRecord) {
        boolean flag = this.maintainRecordService.insertMaintainRecord(maintainRecord);
        if (flag) {
            return ResultDto.MR_ADD_SUCCESS;
        } else {
            return ResultDto.MR_ADD_FAIL;
        }
    }
    @PostMapping(value = "/maintainRecords/update.do")
    public ResultDto updateMaintainRecord(MaintainRecord maintainRecord) {
        boolean flag = this.maintainRecordService.updataMaintainRecord(maintainRecord);
        if (flag) {
            return ResultDto.MR_UPDATE_SUCCESS;
        } else {
            return ResultDto.MR_UPDATE_FAIL;
        }
    }
}
